package com.amado.course50java8;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class Java812Map {

    public static void main(String[] args) {
        Map<String, String> map = new ConcurrentHashMap<>();
        map.put("a","c");
        map.computeIfPresent("d",(k,v)->"abc");
        System.out.println(map);
        System.out.println("----------------");
        preJava8();
        inJava8();
    }

    /**
     * JAVA 8之前
     */
    private static void preJava8() {
        List<String> animals = Arrays.asList("dog", "cat", "cat", "dog", "fish", "dog");
        Map<String, Integer> map = new HashMap<>();
        for(String animal : animals){
            Integer count = map.get(animal);
            map.put(animal, count == null ? 1 : ++count);
        }
        System.out.println(map);
    }
    /**
     * java 8
     */
    private static void inJava8() {
        List<String> animals = Arrays.asList("dog", "cat", "cat", "dog", "fish", "dog");
        Map<String, Integer> map = new HashMap<>();
        for(String animal : animals){
            map.compute(animal, (k, v) -> v == null ? 1 : ++v);
        }
        System.out.println(map);
    }
}
